import i18n from 'i18next';
import {initReactI18next} from 'react-i18next';

import * as en from './en';
import * as fr from './fr';
import * as zhHans from "./zh-Hans";

type TupleUnion<U extends string, R extends unknown[] = []> = {
    [S in U]: Exclude<U, S> extends never
        ? [...R, S]
        : TupleUnion<Exclude<U, S>, [...R, S]>;
}[U];

const ns = Object.keys(zhHans) as TupleUnion<keyof typeof zhHans>;

export const defaultNS = ns[0];

void i18n.use(initReactI18next).init({
    ns,
    defaultNS,
    resources: {
        en,
        fr,
        zhHans
    },
    // 默认语言
    lng: 'zhHans',
    // 回退语言
    fallbackLng: 'en',
    interpolation: {
        escapeValue: false, // not needed for react as it escapes by default
    },
    saveMissing: true,
    compatibilityJSON: 'v3',
});

/*
// 使用方式
const { t, i18n } = useTranslation();
// 切换语言
const changeLanguage = () => {
    i18n.changeLanguage(i18n.language === 'en' ? 'zh' : 'en');
};
*/

export default i18n;
